#### Individual estimates for each independent variable ####

# CA local elections
# surnames
mod_ced_surname_pro <- lm(formula(paste(dv_ced, "~", iv_last_algorithm, "+", ced_controls)), data = ced)
mod_ced_surname_pro <- get_clusters(mod_ced_surname_pro)
mod_ced_surname_pro <- tidy(mod_ced_surname_pro) %>% 
  mutate(data = "Local Elections",
         var = "Surname Pronounceability")

mod_ced_surname_common <- lm(formula(paste(dv_ced, "~", iv_last_commonality, "+", ced_controls)), data = ced)
mod_ced_surname_common <- get_clusters(mod_ced_surname_common)
mod_ced_surname_common <- tidy(mod_ced_surname_common) %>% 
  mutate(data = "Local Elections",
         var = "Surname Commonality")

# first names
mod_ced_fname_pro <- lm(formula(paste(dv_ced, "~", iv_first_algorithm, "+", ced_controls)), data = ced)
mod_ced_fname_pro <- get_clusters(mod_ced_fname_pro)
mod_ced_fname_pro <- tidy(mod_ced_fname_pro) %>% 
  mutate(data = "Local Elections",
         var = "First Name Pronounceability")


mod_ced_fname_common <- lm(formula(paste(dv_ced, "~", iv_first_commonality, "+", ced_controls)), data = ced)
mod_ced_fname_common <- get_clusters(mod_ced_fname_common)
mod_ced_fname_common <- tidy(mod_ced_fname_common) %>% 
  mutate(data = "Local Elections",
         var = "First Name Commonality")

# full names
mod_ced_fullname_pro <- lm(formula(paste(dv_ced, "~", iv_full_name_algorithm, "+", ced_controls)), data = ced)
mod_ced_fullname_pro <- get_clusters(mod_ced_fullname_pro)
mod_ced_fullname_pro <- tidy(mod_ced_fullname_pro) %>% 
  mutate(data = "Local Elections",
         var = "Full Name Pronounceability")

mod_ced_fullname_common <- lm(formula(paste(dv_ced, "~", iv_full_name_commonality, "+", ced_controls)), data = ced)
mod_ced_fullname_common <- get_clusters(mod_ced_fullname_common)
mod_ced_fullname_common <- tidy(mod_ced_fullname_common) %>% 
  mutate(data = "Local Elections",
         var = "Full Name Commonality")

ced_estimates <- rbind(mod_ced_surname_pro, mod_ced_surname_common, mod_ced_fname_pro, mod_ced_fname_common, mod_ced_fullname_pro, mod_ced_fullname_common)

# Primary elections
# surnames
mod_primary_surname_pro <- lm(formula(paste(dv_prim, "~", iv_last_algorithm, "+", primary_controls)), data = primary)
mod_primary_surname_pro <- get_clusters(mod_primary_surname_pro)
mod_primary_surname_pro <- tidy(mod_primary_surname_pro) %>% 
  mutate(data = "Primary Elections",
         var = "Surname Pronounceability")

mod_primary_surname_common <- lm(formula(paste(dv_prim, "~", iv_last_commonality, "+", primary_controls)), data = primary)
mod_primary_surname_common <- get_clusters(mod_primary_surname_common)
mod_primary_surname_common <- tidy(mod_primary_surname_common) %>% 
  mutate(data = "Primary Elections",
         var = "Surname Commonality")

# first names
mod_primary_fname_pro <- lm(formula(paste(dv_prim, "~", iv_first_algorithm, "+", primary_controls)), data = primary)
mod_primary_fname_pro <- get_clusters(mod_primary_fname_pro)
mod_primary_fname_pro <- tidy(mod_primary_fname_pro) %>% 
  mutate(data = "Primary Elections",
         var = "First Name Pronounceability")

mod_primary_fname_common <- lm(formula(paste(dv_prim, "~", iv_first_commonality, "+", primary_controls)), data = primary)
mod_primary_fname_common <- get_clusters(mod_primary_fname_common)
mod_primary_fname_common <- tidy(mod_primary_fname_common) %>% 
  mutate(data = "Primary Elections",
         var = "First Name Commonality")

# full names
mod_primary_fullname_pro <- lm(formula(paste(dv_prim, "~", iv_full_name_algorithm, "+", primary_controls)), data = primary)
mod_primary_fullname_pro <- get_clusters(mod_primary_fullname_pro)
mod_primary_fullname_pro <- tidy(mod_primary_fullname_pro) %>% 
  mutate(data = "Primary Elections",
         var = "Full Name Pronounceability")

mod_primary_fullname_common <- lm(formula(paste(dv_prim, "~", iv_full_name_commonality, "+", primary_controls)), data = primary)
mod_primary_fullname_common <- get_clusters(mod_primary_fullname_common)
mod_primary_fullname_common <- tidy(mod_primary_fullname_common) %>% 
  mutate(data = "Primary Elections",
         var = "Full Name Commonality")

primary_estimates <- rbind(mod_primary_surname_pro, mod_primary_surname_common, mod_primary_fname_pro, mod_primary_fname_common, mod_primary_fullname_pro, mod_primary_fullname_common)

# General elections
# surnames
mod_general_surname_pro <- lm(formula(paste(dv_gen, "~", iv_last_algorithm, "+", general_controls)), data = general)
mod_general_surname_pro <- get_clusters(mod_general_surname_pro)
mod_general_surname_pro <- tidy(mod_general_surname_pro) %>% 
  mutate(data = "General Elections",
         var = "Surname Pronounceability")

mod_general_surname_common <- lm(formula(paste(dv_gen, "~", iv_last_commonality, "+", general_controls)), data = general)
mod_general_surname_common <- get_clusters(mod_general_surname_common)
mod_general_surname_common <- tidy(mod_general_surname_common) %>% 
  mutate(data = "General Elections",
         var = "Surname Commonality")

# first names
mod_general_fname_pro <- lm(formula(paste(dv_gen, "~", iv_first_algorithm, "+", general_controls)), data = general)
mod_general_fname_pro <- get_clusters(mod_general_fname_pro)
mod_general_fname_pro <- tidy(mod_general_fname_pro) %>% 
  mutate(data = "General Elections",
         var = "First Name Pronounceability")

mod_general_fname_common <- lm(formula(paste(dv_gen, "~", iv_first_commonality, "+", general_controls)), data = general)
mod_general_fname_common <- get_clusters(mod_general_fname_common)
mod_general_fname_common <- tidy(mod_general_fname_common) %>% 
  mutate(data = "General Elections",
         var = "First Name Commonality")

# full names
mod_general_fullname_pro <- lm(formula(paste(dv_gen, "~", iv_full_name_algorithm, "+", general_controls)), data = general)
mod_general_fullname_pro <- get_clusters(mod_general_fullname_pro)
mod_general_fullname_pro <- tidy(mod_general_fullname_pro) %>% 
  mutate(data = "General Elections",
         var = "Full Name Pronounceability")

mod_general_fullname_common <- lm(formula(paste(dv_gen, "~", iv_full_name_commonality, "+", general_controls)), data = general)
mod_general_fullname_common <- get_clusters(mod_general_fullname_common)
mod_general_fullname_common <- tidy(mod_general_fullname_common) %>% 
  mutate(data = "General Elections",
         var = "Full Name Commonality")

general_estimates <- rbind(mod_general_surname_pro, mod_general_surname_common, mod_general_fname_pro, mod_general_fname_common, mod_general_fullname_pro, mod_general_fullname_common)

estimates <- rbind(ced_estimates, primary_estimates, general_estimates) %>%
  filter(str_detect(term, "scale") == T) %>% 
  mutate(highbound = estimate + std.error*1.96,
         lowbound = estimate - std.error*1.96) %>% 
  mutate(term = case_when(term == "scale(percent_freq)" ~ "Surname Commonality",
                          term == "scale(last_algorithm)" ~ "Surname Pronounceability",
                          term == "scale(fprop)" ~ "First Name Commonality",
                          term == "scale(first_algorithm)" ~ "First Name Pronounceability",
                          term == "scale(full_name_commonality)" ~ "Full Name Commonality",
                          term == "scale(full_name_algorithm)" ~ "Full Name Pronounceability"))

estimates$term = factor(estimates$term, levels = c("First Name Commonality", 
                                                   "Surname Commonality",
                                                   "Full Name Commonality",
                                                   "First Name Pronounceability",
                                                   "Surname Pronounceability",
                                                   "Full Name Pronounceability"))

ggplot(estimates,
       aes(x = estimate,
           y = term,
           color = data)) +
  geom_point(position = position_dodge(width = .5)) +
  geom_errorbarh(aes(xmax = highbound,
                     xmin = lowbound),
                 height = 0,
                 position = position_dodge(width = .5)) +
  scale_color_manual(values = c("#1b9e77",
                                "#d95f02",
                                "#7570b3")) +
  xlim(c(-1, 2)) +
  geom_vline(xintercept = 0, linetype = "dotted") +
  xlab("Change in vote share per 1 SD increase in name fluency") +
  ylab("") +
  theme_classic() +
  theme(text = element_text(family = "Times New Roman"),
        panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.title= element_blank(),
        legend.position = "bottom",
        axis.title.x  = element_text(family = "Times New Roman"))

ggsave("meet-journal-requirements/figures/estimates-foreach-iv.pdf", width = 6 , height = 6, units = "in", dpi = 600, device = cairo_pdf)
